Namespacing everything to /UVa.
[andmenj-acm.git] / UVa / 11451 - Water restrictions / 11451.cpp
blob9d9b468c6e6885af801fe016edfb716527f54ed7
1 #include <iostream>
2 #include <vector>
4 using namespace std;
6 int pos[10], m[10], C, best, S;
8 void backtrack(vector<int> &n){
9 for (int i=0, sum = 0; i<S; ++i){
10 sum += n[i];
11 if (sum > C) return;
14 int mask = 0;
15 for (int i=0; i<S; ++i){
16 if (n[i] > 0){
17 for (int j=pos[i]-n[i]; j<= pos[i]+n[i]; ++j){
18 mask = mask | (1<<j);
23 best >?= __builtin_popcount(mask);
25 for (int i=0; i<S; ++i){
26 if (n[i]+1 <= m[i]){
27 n[i]++;
28 backtrack(n);
29 n[i]--;
34 int main(){
35 int T;
36 cin >> T;
37 while (T--){
38 int L;
39 cin >> L >> S;
40 for (int i=0; i<S; ++i){
41 cin >> pos[i];
43 cin >> C;
44 for (int i=0; i<S; ++i){
45 cin >> m[i];
48 best = -1;
49 vector<int> n(S, 0);
50 backtrack(n);
51 cout << best << endl;
54 return 0;